package com.edu.cdp.controller;

import com.edu.cdp.DTO.AdminDTO;
import com.edu.cdp.DTO.AvatarDTO;
import com.edu.cdp.DTO.LoginDTO;
import com.edu.cdp.VO.AdminVo;
import com.edu.cdp.common.Result;
import com.edu.cdp.domain.Admin;
import com.edu.cdp.service.AdminService;
import com.edu.cdp.service.CommonService;
import com.edu.cdp.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Tag(name="管理员相关接口")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录")
    public Result<AdminVo> login(@RequestBody LoginDTO loginDto){
        System.out.println("登录："+ loginDto.getUsername() +" /n"+loginDto.getUsername());
        Admin admin = adminService.login(loginDto.getUsername(), DigestUtils.md5DigestAsHex(loginDto.getPassword().getBytes()));

        if (admin != null){
            Map<String, Object> claims = new HashMap<String, Object>();
            claims.put("id", admin.getId());
            claims.put("username", admin.getName());

            String token = JwtUtil.genToken(claims);
            AdminVo adminVo = new AdminVo();
            BeanUtils.copyProperties(admin, adminVo);
            adminVo.setToken(token);
            return Result.success(adminVo);
        } else {
            return Result.error("账户或密码错误");
        }
    }

    @PostMapping("/register")
    @Operation(summary = "添加管理员",
            description = "此接口允许管理员添加其个人信息。"
    )
    public Result<Admin> register(@ParameterObject AdminDTO admin){
        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
        admin.setPassword(password);
        Admin result = adminService.add(admin);
        if (result != null) {
            return Result.success(result);
        } else {
            return Result.error("注册失败");
        }
    }

    @PutMapping("/update")
    @Operation(summary = "修改管理员",
    description = "此接口允许管理员更新其个人信息。"
    )
    public Result<Admin> update(@RequestBody AdminDTO admin){
        Admin newAdmin = adminService.newUpdate(admin);
        if (newAdmin == null) {
            return Result.error("查无此管理员");
        } else {
            boolean result = adminService.updateById(newAdmin);
            if (result) {
                return Result.success(newAdmin);
            } else {
                return Result.error("修改失败");
            }
        }
    }

    @PostMapping("/avatar")
    @Operation(summary = "更新头像",
            description = "此接口用于管理员更新头像")
    public Result<Admin> avatar(@RequestBody AvatarDTO avatarDTO){
        Admin admin = adminService.updateAvatar(avatarDTO.getId(), avatarDTO.getAvatar());
        if (admin != null) {
            return Result.success(admin);
        } else {
            return Result.error("查无此管理员");
        }

    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除管理员")
    public Result<Admin> delete(
            @Parameter(name = "id", description = "管理员id")
            Integer id){
        boolean result = adminService.removeById(id);
        if (result) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }
}
